﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CharmL {
    public class HashTable {
        //        typedef HASHNS::hash_multimap<int, cHashItem *,   
        //   HASHNS::hash<int>, equal_to<int> > cHTable;
        //typedef pair<cHTable::iterator, cHTable::iterator> cHTFind;
        //typedef cHTable::value_type cHTPair;
        public Dictionary<int, List<HashItem>> HTable;

        public bool Add(List<int> iSet, int lit, int iSup, int ihVal) {
            HashItem hItem = new HashItem(iSet, lit, iSup, ihVal);
            bool res = ListAdd(hItem);
            if (res) {

                return false;
            } else return true;
            #region C++
            //cHashItem* Hitem = new cHashItem(iset, lit, isup, ihval);
            //bool res = list_add(Hitem);
            //if (res) {
            //    delete Hitem;
            //    return false;
            //} else return true;
            #endregion
        }


        public bool ListAdd(HashItem Hitem) {
            if (HTable.ContainsKey(Hitem.HashVal)) {
                List<HashItem> hashItems = HTable[Hitem.HashVal];
                foreach (HashItem item in hashItems) {
                    if (HashItem.Compare(item, Hitem) == 0) {
                        return true;
                    }
                }
            } else {
                HTable.Add(Hitem.HashVal, new List<HashItem>());
            }
            HTable[Hitem.HashVal].Add(Hitem);
            return true;            
            //            bool cHashTable::list_add(cHashItem *Hitem)
            //{

            //   int hres = chtable.hash_funct()(Hitem->hashval());

            //   //cout << "HRES " << Hitem->hashval() << " "<< hres << endl;

            //   cHTFind p = chtable.equal_range(hres);
            //   cHTable:: iterator hi = p.first;

            //   int res;
            //   for (; hi!=p.second; ++hi){
            //      res = cHashItem::compare(Hitem, (*hi).second);

            //      //cout << "LOOK AT " << res << " -- " << *(*li) << endl;

            //      if (res == 0) return true;
            //   }
            //   chtable.insert(cHTPair(hres, Hitem));
            //   return false;
            //}
        }

        public void PrintHashStats() {
            //TODO
            //void print_hashstats(){
            //   cout << "HASHSTATS " << chtable.size() << " " 
            //        << chtable.bucket_count() << endl;
            //}
        }
    }
}
